package com.qf.fmall.order.controller;

import com.qf.fmall.order.entity.Orders;
import com.qf.fmall.order.service.IOrdersService;
import com.qf.fmall.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.ParseException;
import java.util.Arrays;
import java.util.HashMap;

/**
 * <p>
 * 订单  前端控制器
 * </p>
 *
 * @author djl
 * @since 2024-04-30
 */
@RestController
@RequestMapping("/order")
@Slf4j
@CrossOrigin
public class OrdersController {

    @Autowired
    private IOrdersService ordersService;

    @GetMapping("/status/{orderid}")
    public ResultVo status(@PathVariable("orderid") String orderid){

        log.info("收到的参数为,orderid={}",orderid);

        String status =  ordersService.queryOrder(orderid);

        return new ResultVo(10000, "success", status);

    }

    @PostMapping("/add")
    public ResultVo add(Integer[] cids,@RequestBody @Valid Orders order) throws ParseException {

        //1. 打印日志
        log.info("接收到的参数为，cids={}，order={}", Arrays.asList(cids),order);

        //2. 校验参数
        if (cids==null || cids.length<=0){
            throw new RuntimeException("cids参数非法");
        }

        //3. 调用service的方法
        HashMap<String,String> data = ordersService.addOrder(cids,order);

        return new ResultVo(10000,"success",data);
    }

}
